<script lang="ts" setup>
import { ref, nextTick } from 'vue'
import wjzs from '@/components/wjzs.vue'
import { ElMessage } from 'element-plus'
import jdfkService from '@/api/jdfk'

const emit = defineEmits(['refresh'])
/*
 * 打开弹框
 */
const dialogVisible = ref<boolean>(false)
// 入参
const wjzsRef = ref<InstanceType<typeof wjzs>>()
const dialogTitle = ref('')
const wsUrl = ref<string>('')
const reqParams = ref<any>()
const isView = ref(false)
// 生命周期
const openDialog = (url: any, title: string, params: any, view: boolean) => {
  dialogTitle.value = title
  wsUrl.value = url
  reqParams.value = params
  dialogVisible.value = true
  isView.value = view
  nextTick(() => {
    wjzsRef.value?.init()
  })
}

const loading = ref(false)
const save = () => {
  loading.value = true
  jdfkService
    .addGdjdtzs(reqParams.value)
    .then(() => {
      ElMessage.success('保存成功')
      dialogVisible.value = false
      emit('refresh')
    })
    .catch((err) => {
      !err.isError && ElMessage({ type: 'warning', message: err.message })
    })
    .finally(() => {
      loading.value = false
    })
}

defineExpose({ openDialog })
</script>
<template>
  <el-dialog
    v-model="dialogVisible"
    :title="dialogTitle"
    width="80%"
    :close-on-click-modal="false"
    :append-to-body="true"
  >
    <div class="dialog-box">
      <!--文件-->
      <wjzs ref="wjzsRef" wjlx="pdf" :url="wsUrl" />
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!isView" type="primary" @click="save" :loading="loading">保存</el-button>
        <el-button @click="dialogVisible = false">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style lang="scss" scoped>
.dialog-box {
  width: 100%;
  height: 70vh;
}
</style>
